﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Sales
{
  public   class CloseWindow
    {

        #region 模拟关闭messagebox
        [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        public const int WM_CLOSE = 0x10;

        public  static  void KillMessageBox(string handle)
        {
            //按照MessageBox的标题，找到MessageBox的窗口  
            IntPtr ptr = FindWindow(null, handle);
            if (ptr != IntPtr.Zero)
            {
                //找到则关闭MessageBox窗口  
                PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            }
        }
        public  static int FindMessageBox(string handle)
        {
            IntPtr ptr = FindWindow(null, handle);
            if (ptr != IntPtr.Zero)
            {
                return 1;
            }
            else
                return 0;
        }
        #endregion


    }
}
